﻿using DuelQuizz.contract;
using DuelQuizzServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DuelQuizz.Views
{
    /// <summary>
    /// Logique d'interaction pour AddQuestion.xaml
    /// </summary>
    public partial class AddQuestion : UserControl
    {
        public List<Category> cat;
        public List<Category> Cat
        {
            get { return cat; }
            set { cat = value; }
        }

        public MainPage mp;
        

        public AddQuestion(MainPage mp)
        {
            this.mp = mp;
            
            cat = AppServices.getDuelQuizzServices().getAllCategories().ToList();
            InitializeComponent();

            this.Categories.ItemsSource = cat;
            this.Categories.DisplayMemberPath = "name";
            this.Categories.SelectedIndex= 0;      
        }

        private void Button_Click_Valider(object sender, RoutedEventArgs e)
        {
            if (Question.Text == "" || GoodAnswer.Text == "" || BadAnswer1.Text == "" || BadAnswer2.Text == "" || BadAnswer3.Text == "")
            {
                MessageBox.Show("Champ(s) manquant(s)");
                return;
            }
                   

            Category category = this.Categories.SelectedItem as Category;
            Answer goodAns = new Answer();
            goodAns.text = this.GoodAnswer.Text;
            goodAns.right = true;

            Answer badAns1 =  new Answer();
            badAns1.text = BadAnswer1.Text;
            badAns1.right = false;

            Answer badAns2 = new Answer();
            badAns2.text = BadAnswer2.Text;
            badAns2.right = false;

            Answer badAns3 = new Answer();
            badAns3.text = BadAnswer3.Text;
            badAns3.right = false;
 
            Question question = new Question();
            question.text = this.Question.Text;
            question.time_to_respond = 30;
            question.title = "none";
            question.difficulty = 1;
            question.creator = 1;
            
            question.answers.Add(goodAns);
            question.answers.Add(badAns1);
            question.answers.Add(badAns2);
            question.answers.Add(badAns3);

            bool ok = AppServices.getDuelQuizzServices().addQuestion(question, category.id);

            if (ok)
            {
                MessageBox.Show("Question sauvegardée !");
            }
            else
            {
                MessageBox.Show("Erreur, recommencez !");
            }
        }

        private void Button_Click_Retour(object sender, RoutedEventArgs e)
        {
            MainWindow.changeWindows(mp);
        }
    }
}
